package javaobjectsandencapsulation.no6;

import java.util.ArrayList;
import java.util.List;

/**
 * 测试对List应用泛型
 */
public class Test6 {
    public static void main(String[] args) {
        //1.创建多个狗狗对象
        Dog dog = new Dog("欧欧", "雪瑞纳");
        Dog dog1 = new Dog("亚亚", "拉布拉多");
        Dog dog2 = new Dog("美美", "哈士奇");
        Dog dog3 = new Dog("菲菲", "泰迪");
        //2.创建ArrayList集合对象并把多个狗狗对象放入其中
        List<Dog> dogs = new ArrayList<Dog>();    //标记元素类型
        dogs.add(dog);
        dogs.add(dog1);
        dogs.add(dog2);
        dogs.add(dog3);
        dogs.add(0, dog2);   //添加到指定位置
        //dogs.add("hello");    //出现编译错误，元素类型不是Dog
        //3.显示第三个元素的信息
        Dog dogx = dogs.get(2);   //无需类型强制转换
        System.out.println(dogx.getName() + "\t" + dogx.getType());
        System.out.println("\n所有狗狗的信息如下：");
        for (Dog dogxs : dogs) {
            System.out.println(dogxs.getName() + "\t" + dogxs.getType());
        }
    }
}
